using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOGDE.AccesoDatos.Entidades;
using SIOGDE.Negocios;
using SIOGDE.Negocios.Servicios;
using Didsoft.Mvc.DataModel;
using Didsoft.Mvc.Controller;
using System.Globalization;
using System.Threading;

namespace SIOGDE.Web.Controllers
{
    public class PersonaController : BaseController<Persona>
    {
	    #region interface servicios Persona

		private IPersonaNegociosServicio servicio = BusinessFactory.Current.GetPersonaNegocios();

		#endregion
		
		
		#region constructor and overridable

        public PersonaController()
        {
            DropdownListStringsProp = "NombreCompleto";
            base.SetDataModelService(servicio);            
        }
		        

		

		#endregion

		#region	actions
        
		[HttpGet]
		public override ActionResult Index()
        {
            return RedirectToAction("FilterList");
        }

        [HttpGet]
        public ActionResult Relacion()
        {
            return View();
        }

        public ActionResult IrADataForm(int PersonaId)
        { 
            var ide = Didsoft.Mvc.Security.WebUtils.Encrypt(PersonaId.ToString());
          return RedirectToAction("DataForm", new{ide=Didsoft.Mvc.Security.WebUtils.Encrypt(PersonaId.ToString())});
        }


        #region filtrado
        
        [HttpPost]
        public JsonResult JSONDataPager(string filter = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                int numPagina = 1;
                if(jtStartIndex!=0)
                  numPagina = (jtStartIndex / jtPageSize) + 1;

                var items = servicio.PaginarPorFiltro(filter, jtPageSize, numPagina);
                //var items = searchedItems.Skip(jtStartIndex).Take(jtPageSize).ToList();
                return Json(new { Result = "OK", Records = items.Items, TotalRecordCount = items.Count  });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }


        [HttpPost]
        public JsonResult JSONDataPagerSearch(string nombres = "", string apellidoPaterno = "", string apellidoMaterno="", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {                
                var searchedItems = servicio.BuscarPorFiltro(nombres, apellidoPaterno, apellidoMaterno);
                var items = searchedItems.Skip(jtStartIndex).Take(jtPageSize).ToList();
                return Json(new { Result = "OK", Records = items, TotalRecordCount = searchedItems.Count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        #endregion


        #region enviar email

        public ActionResult EnviarEmail(string guid)
        {
            Persona persona = servicio.GetByGuid(guid);
            return View(persona);
        }

        [HttpPost]
        public ActionResult EnviarEmail(FormCollection form)
        {
            try
            {
                string guid = form["Guid"];
                string email = form["Email"];
                servicio.EnviarEmail(guid, email);
                return RedirectToAction("FilterList");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DataFormError", ex.Message);
                return View(servicio.GetByGuid(form["guid"]));                
            }


        }

        #endregion



        public ActionResult Info(string error="")
        {
            if (!string.IsNullOrEmpty(error))
                ModelState.AddModelError("DataFormError", error);



            return View(servicio.GetInfoPersonal());        
        }


        #endregion


    }
}

